package com.whmskj.xjlhsc.mobie.Scheduling.Service.Impl;

import com.whmskj.xjlhsc.mobie.ProductionManagement.mapper.YieldReportsMapper;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.dto.MonthYieldReportsDTO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.MonthYieldReportsVO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.service.imp.YieldReportsServiceImpl;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.EmpDTO;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.EmployeePerformanceDTO;
import com.whmskj.xjlhsc.mobie.Scheduling.Service.EmployeePerformanceService;
import com.whmskj.xjlhsc.mobie.Scheduling.entity.IndividualPerformance;
import com.whmskj.xjlhsc.mobie.Scheduling.entity.Variety;
import com.whmskj.xjlhsc.mobie.Scheduling.mapper.EmployeePerformanceMapper;
import com.whmskj.xjlhsc.mobie.Scheduling.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author jiaruchun
 * @date 2025-09-04 17:26
 */
@Service
public class EmployeePerformanceServiceImpl implements EmployeePerformanceService {
    private static final Logger log = LoggerFactory.getLogger(EmployeePerformanceServiceImpl.class);
    @Autowired
    private EmployeePerformanceMapper employeePerformanceMapper;
    @Autowired
    private YieldReportsMapper yieldReportsMapper;

    @Override
    public EmployeePerformanceControllerVO getEmployeePerformanceControllerVO(EmployeePerformanceDTO employeePerformanceDTO) {
        String month = employeePerformanceDTO.getMonth().split("（")[0];
        String mon = null;//2025-06(2025-05-21晚--2025-06-20早)
        List<MonthYieldReportsVO> monthYieldReport = getMonthYieldReport();
        for (MonthYieldReportsVO monthYieldReportsVO : monthYieldReport) {
            if(monthYieldReportsVO.getMonth().split("（")[0].equals(month)){
                mon = monthYieldReportsVO.getMonth();
            }
        }
        String name = employeePerformanceDTO.getName();
        String process = employeePerformanceDTO.getProcess();
        Integer empCode = employeePerformanceDTO.getEmpCode();
        String empName = employeePerformanceDTO.getEmpName();//工种
        if(name == ""){
            name = null;
        }
        if(empName == ""){
            empName = null;
        }
        if(process == ""){
            process = null;
        }
        List<EmployeePerformanceVo> employeePerformanceControllerVO = employeePerformanceMapper.getEmployeePerformanceControllerVO(month,process,name,empName,empCode);
        for (EmployeePerformanceVo employeePerformanceVo : employeePerformanceControllerVO) {
            //不为null,去除其中的头像url
            if(employeePerformanceVo.getAvatar() != null && !employeePerformanceVo.getAvatar().trim().isEmpty()){
                String avatarJson = employeePerformanceVo.getAvatar();
                // 使用正则表达式提取URL
                Pattern pattern = Pattern.compile("\"url\"\\s*:\\s*\"([^\"]+)\"");
                Matcher matcher = pattern.matcher(avatarJson);
                if (matcher.find()) {
                    String url = matcher.group(1);
                    employeePerformanceVo.setAvatar(url);
                } else {
                    // 如果没有找到URL，设置为空字符串
                    employeePerformanceVo.setAvatar(null);
                }
            } else {
                // 如果avatar为null或空字符串，设置为空字符串
                employeePerformanceVo.setAvatar(null);
            }
        }
        Float monthPerformance = 0.0f;
        for (EmployeePerformanceVo employeePerformanceVo : employeePerformanceControllerVO) {
            monthPerformance += employeePerformanceVo.getPerformance();
        }
        //奖惩金额
        List<Integer> ids = employeePerformanceControllerVO.stream().map(new Function<EmployeePerformanceVo, Integer>() {
            @Override
            public Integer apply(EmployeePerformanceVo employeePerformanceVo) {
                return employeePerformanceVo.getEmployeeId();
            }
        }).collect(Collectors.toList());
        if (ids != null && ids.size() > 0) {
            List<EmployeePerformanVO> rewardAndPunish = employeePerformanceMapper.getRewardAndPunish(ids, month);
            for (EmployeePerformanceVo employeePerformanceVo : employeePerformanceControllerVO) {
                for (EmployeePerformanVO employeePerformanVO : rewardAndPunish) {
                    if(employeePerformanceVo.getEmployeeId().equals(employeePerformanVO.getEmployeeId())){
                        employeePerformanceVo.setRewardAndPunish(employeePerformanVO.getRewardAndPunish());
                    }
                }
            }
        }
        return new EmployeePerformanceControllerVO(mon,monthPerformance, employeePerformanceControllerVO);
    }

    @Override
    public IndividualPerformanceVO getIndividualPerformanceVO(EmpDTO empDTO) {
        //品种列表
        List<Variety> individualPerformanceVO = employeePerformanceMapper.getIndividualPerformanceVO(empDTO);
        log.info("individualPerformanceVO:{}",individualPerformanceVO);
        List<IndividualPerformance> individualPerformanceList = employeePerformanceMapper.getIndividualPerformanceList(empDTO);
        //总绩效
        Float totalPerformance = 0.0f;
        for (IndividualPerformance individualPerformance : individualPerformanceList) {
            totalPerformance += individualPerformance.getPerformance();
        }
        //员工姓名
        String empName = employeePerformanceMapper.getEmpName(empDTO.getEmpId());
        //盘存月
        String mon = null;
        List<MonthYieldReportsVO> monthYieldReport = getMonthYieldReport();
        for (MonthYieldReportsVO monthYieldReportsVO : monthYieldReport) {
            if(monthYieldReportsVO.getMonth().split("（")[0].equals(empDTO.getMonth())){
                mon = monthYieldReportsVO.getMonth();
            }
        }
        //绩效列表
        return new IndividualPerformanceVO(empName,mon,totalPerformance,individualPerformanceVO,individualPerformanceList);
//        return new IndividualPerformanceVO(totalPerformance,null,individualPerformanceList);
    }

    @Override
    public List<WorkByProcessVO> getWorkByProcess(String process) {
        List<WorkByProcessVO> workByProcessVOList = employeePerformanceMapper.getWorkByProcess(process);
        return workByProcessVOList;
    }

    public List<MonthYieldReportsVO> getMonthYieldReport() {
        List<MonthYieldReportsDTO> panCunMonthYieldReports = yieldReportsMapper.getPanCunMonthYieldReports();
        List<MonthYieldReportsVO> monthYieldReportsVOList = new ArrayList<>();
        for (MonthYieldReportsDTO monthYieldReportsDTO : panCunMonthYieldReports) {
            String[] split = monthYieldReportsDTO.getMonth().split("-");
            String r = split[0] + "-" + split[1] + "（" + monthYieldReportsDTO.getOtherOrganiser() + "晚" + "~ " + monthYieldReportsDTO.getEndDate() + "早" + "）";
                monthYieldReportsVOList.add(new MonthYieldReportsVO(r));
        }
        //顺序颠倒
        Collections.reverse(monthYieldReportsVOList);
        return monthYieldReportsVOList;
    }
}
